package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.Transaction;
import csc.daonjpa.java.domain.TransactionState;

@Repository
public class TransactionDAO {
	
	@PersistenceContext
	EntityManager entityManager;
	
	//return transaction history of customer -- customer action
	public List<Transaction> getReceivedTransactionByCustomerId(Long customerId){
		TypedQuery<Transaction> query = entityManager.createQuery("SELECT t FROM " + Transaction.class.getName()
										+ " t WHERE t.receivedCustomer.id = :customer"
										, Transaction.class);
		query.setParameter("customer", customerId);
		List<Transaction> transactionList2 = query.getResultList();
		return transactionList2;
	}
	
	public List<Transaction> getTransferedTransactionByCustomerId(Long customerId){
			List<Transaction> listTransaction = null;
			
		try{
			TypedQuery<Transaction> query = entityManager.createQuery("SELECT t FROM " + Transaction.class.getName()
					+ " t WHERE t.sendCustomer.id = :customer"
					, Transaction.class);
			query.setParameter("customer", customerId);
			listTransaction = query.getResultList();
		} catch(Exception e){
			e.printStackTrace();
		}
		
		return listTransaction;
	}
	
	
	public List<Transaction> getTransactions(Long customerId, String date) {
		
		List<Transaction> transactions = null;
		try{
			Customer customer = entityManager.find(Customer.class, customerId);
			
			TypedQuery<Transaction> query = entityManager
					.createQuery("SELECT t FROM "
							+ Transaction.class.getName()
							+ " t Where t.sendCustomer = :customer and t.date LIKE :date",
							Transaction.class);
			query.setParameter("customer", customer);
			query.setParameter("date", date + "%");
			transactions = query.getResultList();
		}
		catch(Exception e1){
			e1.printStackTrace();
		}
		
		return transactions;
	}

	@Transactional
	public boolean addTransaction(Transaction transaction) {
		boolean res = false;
		try{
			entityManager.persist(transaction);
			entityManager.merge(transaction.getSendCustomer());
			entityManager.merge(transaction.getReceivedCustomer());
			res = true;
		}
		catch(Exception e1){
			res = false;
			e1.printStackTrace();
		}
		return res;
	}
	
	//lsit account nguoi dung gui trong nhung phien giao dich
	public List<Customer> getCustomers(Long customerId){
		List<Customer> customers = null;
		try{
			TypedQuery<Customer> query = entityManager
					.createQuery("SELECT distinct(t.receivedCustomer) FROM "
							+ Transaction.class.getName()
							+ " t Where t.sendCustomer.id= :customerId ",
							Customer.class);
			query.setParameter("customerId", customerId);
			customers = query.getResultList();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return customers;
	}

	public List<Transaction> getAllTransactionList() {
		List<Transaction> lstTransaction = null;
		try{
			TypedQuery<Transaction> query = entityManager
					.createQuery("SELECT t FROM Transaction t", Transaction.class);
			lstTransaction = query.getResultList();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return lstTransaction;
	}

	public Transaction getTransactionDetails(Long transactionId) {
		Transaction transactionDetail = null;
		try{
			transactionDetail=entityManager.find(Transaction.class, transactionId);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return transactionDetail;
	}

	@Transactional
	public void setApproveTransactionById(Transaction transaction) {
		TransactionState state = null;
		
		try{
			entityManager.merge(transaction.getReceivedCustomer());
			state = entityManager.find(TransactionState.class, TransactionState.APPROVED_TRANSACTION);
			transaction.setTransactionState(state);
			entityManager.merge(transaction);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	@Transactional
	public void setDenyTransactionById(Transaction transaction, String reason) {
		TransactionState state = null;
		try{
			entityManager.merge(transaction.getSendCustomer());
			state = entityManager.find(TransactionState.class, TransactionState.DENIED_TRANSACTION);
			transaction.setTransactionState(state);
			transaction.setDenyReason(reason);
			entityManager.merge(transaction);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
}
